' Ping the core.
' If you can reach it, use direct mode. 
' If you can't reach it, use gateway mode.
' Jack Coates, jack@monkeynoodle.org

' Core agent modification added by Ken Carlisle, carlislek@hhmi.org
'%PROGRAMFILES%/LANDesk/Shared Files/cbaroot/broker/brokerconf.xml. 
'The Order key is the one that needs changing
'1 == "Connect using the Management Gateway"
'2 == "Connect directly to LDMS core"
'0 == "Dynamically determine connection route"

option explicit
dim WSHShell, objWMIService, strcoreserver, objservice, arrtargets, intlist
dim strBrokerfile, strnewtext, strtext, objfile, objfso
dim strComputer, strcoreregkey, strmoderegkey, dimmode, objwmi, ping, objping


On Error Resume Next
strComputer="."
'Set intList = UBound(arrTargets) + 1
'arrTargets(intList) = arrTargets(0)
Set WSHShell = WScript.CreateObject("WScript.Shell")  'WshShell object exposes some of the common shell functionalities of Microsoft Windows
Set objWMIService = GetObject("winmgmts:" _
 & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
'msgbox "Fehler: " & err

' Read the core's name from the registry
strCoreRegKey = "HKLM\Software\Intel\LANDesk\LDWM\"
strCoreServer = WSHShell.RegRead (strCoreRegKey & "CoreServer")


' Read the current mode from the registry
strModeRegKey = "HKLM\Software\Intel\LANDesk\WUSER32\"
dimMode = WSHShell.RegRead (strModeRegKey & "Gateway")


' Set name of Remote Control Service
'Set objService = objWMI.Get("Win32_Service.Name='ISSUSER'")


' Set name of Broker Config File
Const ForReading = 1
Const ForWriting = 2
strBrokerfile = "%ProgramFiles%\LANDESK\Shared Files\cbaroot\broker\broker.conf.xml"
Set objFSO = CreateObject("Scripting.FileSystemObject")

' ping the core
Ping = WshShell.Run("ping -n 1 " & strCoreServer, 0, True)
'msgbox "Fehler: " & Ping 
on error resume next
'If objPing.StatusCode = 0 Then
If Ping = 0 Then

	' We're on home net, should be in direct mode.
	' Configure the ldproxy behavior
    If objFSO.FileExists(strBrokerfile) Then
        Set objFile = objFSO.OpenTextFile(strBrokerfile, ForReading)
    	strText = objFile.ReadAll
	    objFile.Close
    	strNewText = Replace(strText, "<order>*", "<order>2")
    	Set objFile = objFSO.OpenTextFile(strBrokerfile, ForWriting)
	    objFile.WriteLine strNewText
    	objFile.Close
    End If

	' Configure the Remote Control Agent
	If dimMode = 1 Then 'Gateway mode is enabled
		'msgbox "Registrykey: " & strModeRegKey & "Gateway"
		WSHShell.RegWrite strModeRegKey & "Gateway", "00000000", "REG_DWORD"
		'wscript.echo "setzte Registry Key auf 0"
		' Restart Remote Control Service
		'objService.StopService()
		Wshshell.Run("cmd /c net stop issuser",0, TRUE)
		WScript.Sleep 10000   ' Pause to allow service to stop
		'objService.StartService()
		Wshshell.Run("cmd /c net start issuser",0, TRUE)
		WScript.Sleep 10000   ' Pause to allow service to start
	End If
	Else
	' We're on foreign net, should be in gateway mode.
	' Configure the ldproxy behavior
    If Exists(strBrokerFile) Then
    	strText = objFile.ReadAll
	    objFile.Close
	    strNewText = Replace(strText, "<order>*", "<order>0")
	    Set objFile = objFSO.OpenTextFile(strBrokerfile, ForWriting)
	    objFile.WriteLine strNewText
	    objFile.Close
    End If

	' Configure the Remote Control Agent
	If dimMode = 0 Then 'Direct mode is enabled
		WSHShell.RegWrite strModeRegKey & "Gateway", "00000001", "REG_DWORD"
		' Restart Remote Control Service
		'objService.StopService()
		Wshshell.Run("cmd /c net stop issuser",0, TRUE)
		WScript.Sleep 10000   ' Pause to allow service to stop
		'objService.StartService()
		Wshshell.Run("cmd /c net start issuser",0, TRUE)
		WScript.Sleep 10000   ' Pause to allow service to start
	End If
End If

' all done
wscript.quit

